package com.integralPerformance.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.integralPerformance.pojo.excel.ServiceInfoExcel;
import com.integralPerformance.pojo.vo.ServiceInfoVo;
import com.integralPerformance.service.impl.ServiceInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


@Slf4j
public class ServiceListener implements ReadListener<ServiceInfoExcel> {
    ServiceInfoServiceImpl serviceInfoService;

    private static final int BATCH_COUNT = 100;
    private List<ServiceInfoExcel> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public List<ServiceInfoExcel> getSameDataList() {
        return sameDataList;
    }

    private List<ServiceInfoExcel> sameDataList =new ArrayList<>();

    public List<ServiceInfoVo> getSaveDataIdList() {
        if(this.saveDataIdList.size()!=0){
            return serviceInfoService.getSaveDataIdList(this.saveDataIdList);
        }
        return null;
    }

    //保存保存之后的id。
    private List<Integer> saveDataIdList = new ArrayList<>();


    public ServiceListener(ServiceInfoServiceImpl serviceInfoService) {
        this.serviceInfoService = serviceInfoService;
    }

    @Override
    public void invoke(ServiceInfoExcel serviceInfoExcel, AnalysisContext analysisContext) {

        if(serviceInfoExcel.getTeacherName()!=null&&!serviceInfoExcel.getTeacherName().equals("")){
            cachedDataList.add(serviceInfoExcel);
            log.info("解析到一条有效数据:{}", JSON.toJSONString(serviceInfoExcel));
        }

        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
        log.info("所有数据解析完成");
    }

    private void  saveData(){
        //数据校验
        for (int i = 0; i < cachedDataList.size(); i++) {
            if(serviceInfoService.judge(cachedDataList.get(i))){
                sameDataList.add(cachedDataList.get(i));
                log.info("存在相同数据{}已跳过！",JSON.toJSONString(cachedDataList.get(i)));
                cachedDataList.remove(i);

            }
            String teacherId = serviceInfoService.getTeacherId(cachedDataList.get(i).getTeacherName());
            String serviceId = serviceInfoService.getServiceBaseId(cachedDataList.get(i).getServiceBaseName());

            if(teacherId==null || serviceId== null){
                sameDataList.add(cachedDataList.get(i));
                log.info("数据校验不通过{}，已忽略！",JSON.toJSONString(cachedDataList.get(i)));
                cachedDataList.remove(i);

            }

        }
        if (cachedDataList.size()!=0){
            saveDataIdList= serviceInfoService.saveList(cachedDataList);
            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
            log.info("存储数据库成功！");
        }else {
            log.info("无有效数据！");
        }


    }





}
